package rpg.writer.app.player;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import rpg.client.NewCharacterClient;
import rpg.client.model.Actor;
import rpg.client.model.Actor.Gender;
import rpg.engine.client.GameEngineFacade;

public class CreateCharacterJPanel extends JPanel implements NewCharacterClient, ActionListener {
    private static void debug(Object o) {
        System.out.println("CreateCharacterJPanel: " + o);
    }

    private final ClientJPanel client;
    private volatile GameEngineFacade engine = null;

    private final JButton btnDone = new JButton("Create");
    private final JTextField tfName = new JTextField("Jayden");

    public CreateCharacterJPanel(ClientJPanel clientJPanel) {
        super(new GridBagLayout());

        client = clientJPanel;

        tfName.setPreferredSize(new Dimension(200, 20));

        GridBagConstraints gbc = null;

        gbc = new GridBagConstraints();
        add(new JLabel("Name:"), gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        add(tfName, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        add(btnDone, gbc);

        // Add Listeners
        btnDone.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        debug(e);
        if (e.getSource() == btnDone) {
            Actor player = createCharacter();
            client.play(player);
        }
    }

    @Override
    public Actor createCharacter() {
        Actor.Builder ab = new Actor.Builder();
        ab.id("");
        ab.name(tfName.getText());
        ab.gender(Gender.MALE);
        ab.hairColour(Color.BLACK);
        return engine.registerPlayer(ab);
    }

    public void setEngine(GameEngineFacade facade) {
        engine = facade;

        // Should probably load guidance for creating a new character...
    }
}
